﻿using System;

namespace Creventive.Wpf.ShellFactory.Workspace.Primitives
{
    /// <summary>
    /// Represents the context of an application view.
    /// </summary>
    public class ApplicationViewContext
    {
        private IApplicationViewSource source;
        private ApplicationView view;

        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationViewContext"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        public ApplicationViewContext(IApplicationViewSource source)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            this.source = source;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationViewContext"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        public ApplicationViewContext(ApplicationView view)
        {
            if (view == null)
                throw new ArgumentNullException("view");
            this.view = view;
        }

        /// <summary>
        /// Gets a value indicating whether the view this context relates to has been loaded.
        /// </summary>
        /// <value><c>true</c> if this instance is loaded; otherwise, <c>false</c>.</value>
        public bool IsLoaded
        {
            get { return this.view != null; }
        }

        /// <summary>
        /// Gets the source of this context.
        /// </summary>
        /// <value>The source.</value>
        public IApplicationViewSource Source
        {
            get { return this.source; }
        }

        /// <summary>
        /// Gets the view this context relates to. If the view has not been loaded yet, it will be loaded here.
        /// </summary>
        /// <value>The view.</value>
        public ApplicationView View
        {
            get
            {
                if (this.view == null)
                    this.view = this.source.Load();
                return this.view;
            }
        }
    }
}